package com.example.howard.xiao7music.ui.adapter.RemoteAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.howard.xiao7music.R;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Map;

/**
 * Created by Howard on 2016/10/26.
 */
public class RankingSongAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    public RankingSongCallback callback;
    public List<Map<String, Object>> list;

    public RankingSongAdapter(Context c,List<Map<String, Object>> songlist, RankingSongCallback callback){

        this.inflater=LayoutInflater.from(c);
        this.list=songlist;
        this.callback=callback;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView,
                        ViewGroup parent)  {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.ranking_song_item, null);
            viewHolder = new ViewHolder();
            viewHolder.song_Image = (ImageView) convertView
                    .findViewById(R.id.ranking_song_image);
            viewHolder.song_name = (TextView) convertView
                    .findViewById(R.id.ranking_song_song_name);
            viewHolder.siner_name = (TextView) convertView
                    .findViewById(R.id.ranking_song_singer_name);
            viewHolder.download= (ImageButton) convertView
                    .findViewById(R.id.rangking_song_downloadbty);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Picasso.with(convertView.getContext()).load(list.get(position).get("LOGO").toString()).into(viewHolder.song_Image);
        viewHolder.song_name.setText(list.get(position).get("SONG").toString());
        viewHolder.siner_name.setText(list.get(position).get("SINGER").toString());

        viewHolder.download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.download(position);
            }
        });

        return convertView;
    }

    class ViewHolder {
        ImageView song_Image;
        TextView song_name;
        TextView siner_name;
        ImageButton download;

    }

    public interface RankingSongCallback {

        void download (int position);
    }

}
